<?php
namespace app\admin\controller;
use think\Controller;
use think\Loader;
class Article extends Controller
{
	//文章列表页面
	public function article_list()
	{
		$article=Loader::model('article');
		$list=$article/*->where('status',1)*/->paginate(10);
		$page=$list->render();
		$this->assign('page',$page);
		$this->assign('list',$list);
		return $this->fetch('article_list');
	}

	//文章添加页面
	public function article_add()
	{
		if(isset($_POST['submit'])){
			$article=Loader::model('article');
			$data=
			[
				'title'    => $_POST['title'],
				'content'  => $_POST['content'],
				'category' => $_POST['category']
			];
			$article->data($data);
			$exc=$article->save();
			if($exc){
				$this->success('添加成功,正在跳转...');
			}else $this->error('添加失败，正在跳转...');
		}
		//遍历文章类别
		$category=Loader::model('category');
		$list=$category->select();
		$this->assign('list',$list);
		return $this->fetch('article_add');
	}

	//查看文章内容页面
	public function article_read()
	{
		$article=Loader::model('article');
		if(isset($_GET['id']) && !empty($_GET['id'])){
			$id=$_GET['id'];
			$data=$article->where('Id',$id)->select();
			$this->assign('data',$data);
			return $this->fetch();
		}else{
			$this->error('未接收有效参数');
		}
	}
	//编辑文章页面
	public function article_edit()
	{	
		if(isset($_GET['id']) && !empty($_GET['id'])){
			//在文本框显示修改前的内容
			$id=$_GET['id'];
			$article=Loader::model('article');
			$data=$article->where('Id',$id)->select();
			$this->assign('data',$data);
			//遍历文章类别
			$category=Loader::model('category');
			$list=$category->select();
			$this->assign('list',$list);
			//通过POST提交向数据库提交修改
		}else $this->error('请传递有效参数！','article_list');
		return $this->fetch();
	}
	//提交修改后的文章
	public function edit_post()
	{
		if(isset($_POST['submit'])){
			$article=Loader::model('article');
			$exc=$article->where('Id',$_POST['id'])
			->update(
				[
					'title' => $_POST['title'],
					'category' => $_POST['category'],
					'content' => $_POST['content'],
				]
			);
			if($exc){
				$this->success('修改成功','article_list');
			}else $this->success('修改失败！');
		}
	}

	//删除文章页面
	public function article_delete()
	{
		$id=input('get.id');
		$article=Loader::model('article');
		$delete=$article->where('Id',$id)->delete();
		if ($delete) {
			$this->success('删除成功!');		}
		}
		//文章分类列表页面
		public function category_list()
		{
			$category=Loader::model('category');
			$list=$category->paginate(10);
			$page=$list->render();
			$this->assign('list',$list);
			$this->assign('page',$page);
			return $this->fetch('category_list');
		}

	//添加文章分类页面
		public function category_add()
		{
			if(isset($_POST['submit'])){
				if(!empty($_POST['category'])){
					$category=Loader::model('category');
					$data=$_POST['category'];
					$arr=['category'=>$data];
					$category->data($arr);
					$exc=$category->save();
					if($exc){
						$this->success($_POST['category']);
					}else $this->error('添加失败');
				}
			}
			return $this->fetch('category_add');
		}
	//删除文章
		public function art_del()
		{
			$id=input('post.id');
		// echo $id;
			$article=new Art;
			$exc=$article
			->where(['id'=>$id])
			->delete();
			if($exc){
				return json(['code'=>'0','msg'=>'删除成功！']);
			}else 			return json(['code'=>'1','msg'=>'删除失败！']);

		}
		public function demo_add()
		{
			$article=Loader::model('article');
			for ($i=0; $i <20 ; $i++) { 
				$data[]=
				[
					'title'=>'文章'.$i,
					'type'=>'1',
					'from'=>'自拟',
					'content'=>'MEMO',
					'author'=>'普子',
					'abstract'=>'摘要',
					'status'=>'1'
				];
			}
			$article->saveAll($data);
		}

		//批量删除
		public function batch_del()
		{
			$arr=input('post.');
		//$str=implode(",", $arr);
		//echo $str;
		// $article=new Art;
			$del=$this->article->where('id','in',$arr['id'])->delete();
			if($del){
				return json(['code'=>'0','msg'=>'删除成功！']);
			}else return json(['code'=>'1','msg'=>'删除失败！']);

		}
	}